<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<script type="text/javascript">
    var rowId = 0;
    
    $('.ui-reset , #refresh_matrixcriteriaGrid').click(function(){
        $("#matrixcriteriaGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#matrixcriteriaGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#matrixcriteriaGrid").trigger("reloadGrid");
    });

    $.subscribe('matrixcriteriaGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#matrixcriteriaGrid_edit" ).button( "option", "disabled", false );
        $( "#matrixcriteriaGrid_delete" ).button( "option", "disabled", false );
    });

    $.subscribe('searchMatrixcriteriaGrid', function(event,data) {
        $("#matrixcriteriaGrid").jqGrid('searchGrid', {drag:true,closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search Employee"} );
    });

    $.subscribe('addMatrixcriteriaGrid', function(event,data) {
        $("#matrixcriteriaGrid").jqGrid('editGridRow',"new",{drag:true,closeAfterEdit:false,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editMatrixcriteriaGrid', function(event,data) {
        if(rowId!=0){
            $("#matrixcriteriaGrid").jqGrid('editGridRow',rowId,{drag:true,closeAfterEdit:true,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select an category first.");
        }
    });

    $.subscribe('deleteMatrixcriteriaGrid',function(event,data){
        if(rowId!=0){
            $("#matrixcriteriaGrid").jqGrid('delGridRow',rowId,{drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:true});
        }else{
            alert("Sorry. Please select an category first.");
        }
    });
    
    $.subscribe('matrixcriteriaGridComplete',function(event,data){
        $( "#matrixcriteriaGrid_search" ).button( "option", "disabled", false );
        $( "#matrixcriteriaGrid_add" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,'Done.'];
    }
</script>
<div class="gridOptsContainer">
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcriteriaGrid_search" onClickTopics="searchMatrixcriteriaGrid" value="Search Criteria"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcriteriaGrid_add" onClickTopics="addMatrixcriteriaGrid" value="Add Criteria"/>
    <sj:submit button="true" cssStyle="font-size:14px;" disabled="true" id="matrixcriteriaGrid_edit" onClickTopics="editMatrixcriteriaGrid" value="Edit Criteria"/>
    <sj:submit button="true" cssStyle="font-size:14px" disabled="true" id="matrixcriteriaGrid_delete" onClickTopics="deleteMatrixcriteriaGrid" value="Delete Criteria"/>
</div>
<s:url id="remoteurl" action="grid-matrixcriteria"/>
<s:url id="editurl" action="edit-matrixcriteria"/>
<sjg:grid
    id="matrixcriteriaGrid"
    caption="Cashmate Matrix Criteria"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    pager="true"
    pagerButtons="true"
    pagerInput="true"
    rowList="10,15,20"
    rowNum="10"
    loadingText="Requesting Data..."
    width="970"
    shrinkToFit="true"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="false"
    navigatorView="true"
    onSelectRowTopics="matrixcriteriaGridRowSelected"
    onGridCompleteTopics="matrixcriteriaGridComplete"
    >

    <sjg:gridColumn name="criteriaid"
                    key="true"
                    index="criteriaid"
                    title="criteriaid (Not Editable)"
                    sortable="false"
                    hidden="true"
                    />

    <sjg:gridColumn name="criterianame"
                    index="criterianame"
                    title="Criteria"
                    sortable="true"
                    editable="true"
                    edittype="text"
                    search="true"
                    editrules="{required:true}"
                    searchoptions="{sopt:['eq']}"
                    editoptions="{maxlength:'45'}"
                    align="center"
                    />

    <sjg:gridColumn name="totalvalue"
                    index="totalvalue"
                    title="Total value"
                    sortable="true"
                    editoptions="{maxlength:'3'}"
                    editrules="{required:true,number:true}"
                    editable="true"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />
</sjg:grid>
